package com.test.clyq.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.clyq.entity.Content;
import com.test.clyq.entity.Users;
import com.test.clyq.service.ContentService;
import com.test.json.JsonUtil;
import com.test.util.FormatExceptionStack;
import com.test.util.ObjectUtils;
import com.test.util.Pagination;
import com.test.util.SessionUtil;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * ContentAction. <br>
 * 评价选项内容管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: suncg@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class ContentAction extends BaseAction {

	/**
	 * 删除评价选项内容
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward deleteContent(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("删除评价选项内容start");
		ContentService contentService = (ContentService) getBean("contentService");
		Users user = SessionUtil.getUserFromSession(request);
		String contentId = getParameter(request, "contentId");
		try {
			if (!ObjectUtils.isNull(user)) {
				contentService.deleteContent(Long.valueOf(contentId), user);
				this.log.debug("删除评价选项内容成功");
				this.log.info("删除评价选项内容end");
				print(response, true);
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("删除评价选项内容:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}

	/**
	 * 获得评价选项内容列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getContents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得评价选项内容列表start");
		ContentService contentService = (ContentService) getBean("contentService");
		String optionId = getParameter(request, "optionId");
		try {
			String start = getParameter(request, "start");
			String limit = getParameter(request, "limit");
			if (StringUtils.isNumeric(start) && StringUtils.isNumeric(limit) && StringUtils.isNumeric(optionId)) {
				int currpageNo = 1;
				int pageCount = Integer.parseInt(limit);
				if (Integer.parseInt(start) / pageCount != 0) {
					currpageNo = Integer.parseInt(start) / pageCount + 1;// 当前页码
				}
				Pagination pagination = new Pagination(currpageNo, pageCount);// 设置分页参数（页码和每页显示记录条数）
				int totalCount = 0;
				List<Content> list = contentService.getContentByPage(pagination, Long.valueOf(optionId));
				totalCount = pagination.getTotalRows();
				String jsonStr = JsonUtil.getJsonStr(list, new String[] { "id", "content", "date" }, new String[] { "date" }, totalCount);
				this.log.debug("获得评价选项内容列表成功");
				this.log.info("获得评价选项内容列表end");
				print(response, jsonStr, null, null);
			}
		}
		catch (Exception e) {
			this.log.error("获得评价选项内容:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

}
